//
//  FCImportPinVC.m
//  FundChain
//
//  Created by 肖浩然的mac on 2018/3/5.
//  Copyright © 2018年 RUIXIN. All rights reserved.
//

#import "FCImportPinVC.h"
#import "FCForgotPinVC.h"
#import <LocalAuthentication/LocalAuthentication.h>
#import "FCTransactionHistoryVC.h"

@interface FCImportPinVC (){
    UIView     *_viewNav;
}
@property (nonatomic,strong) UILabel *titlePinLabel;
@property (nonatomic,strong) UIView *sixPointView;
@property (nonatomic,strong) NSArray *numPointArr;
@property (nonatomic,strong) NSMutableArray *PINArrM;

@property(strong,nonatomic)NSString *passFingerToken;//指纹64token
@property(strong,nonatomic)NSString *fingerToken;
@end

@implementation FCImportPinVC


- (void)viewDidLoad {
    [super viewDidLoad];
    [FCLocalUserHelper saveIsMainVC:@"0"];
    [self setNav];
    self.view.backgroundColor = kWhiteColor;
    //是否显示指纹
    self.passFingerToken = [FCLocalUserHelper getPasswordFingerToken];
    if (!kStringIsEmpty(self.passFingerToken))
    {
        //AES解密
        NSString *pfStr = [FCLocalUserHelper aes256_decryptStr:self.passFingerToken];
        NSString *subStr = [pfStr substringFromIndex:12];
        self.fingerToken = subStr;
        
    }
    self.numPointArr = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"C",@"0",@"X"];
    [self setupUI];
}
#pragma mark-忘记pin
-(void)forgetPinBtnClick{
    FCForgotPinVC *vc = [FCForgotPinVC new];
    [self.navigationController pushViewController:vc animated:YES];
    
}
#pragma mark-设置UI
-(void)setupUI{
    
    
    CGFloat headLabelTop;
    CGFloat headBtnTop;
    CGFloat twoBtnSep;//两个按钮之间的间距
    CGFloat lineViewTop;//第二条分割线
    if(iPhone4)
    {
        headLabelTop = 30*i5_i6H;
        headBtnTop = 30*i5_i6H;
        twoBtnSep = 25*i5_i6H;
        lineViewTop = 105*i5_i6H;
    }
    else if (iPhone5)
    {
        headLabelTop = 30*i5_i6H;
        headBtnTop = 30*i5_i6H;
        twoBtnSep = 25*i5_i6H;
        lineViewTop = 105*i5_i6H;
    }else if(iPhone6)
    {
        headLabelTop = 30;
        headBtnTop = 30;
        twoBtnSep = 25;
        lineViewTop = 105;
    }else if(iPhone6p)
    {
        headLabelTop = 30*i6p_6H;
        headBtnTop = 30*i6p_6H;
        twoBtnSep = 25*i6p_6H;
        lineViewTop = 105*i6p_6H;
    }else
    {
        headLabelTop = 30*iX_i6H;
        headBtnTop = 30*iX_i6H;
        twoBtnSep = 25*iX_i6H;
        lineViewTop = 105*iX_i6H;
    }
    CGFloat leftWidth = SCREEN_WIDTH/2-twoBtnSep*2-twoBtnSep/2-45;
    //设置PIN
    UILabel *titlePinLabel = [UILabel new];
    self.titlePinLabel = titlePinLabel;
    [self.view addSubview:titlePinLabel];
    [titlePinLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            if (iPhoneX) {
                make.centerX.mas_equalTo(0);
                make.top.mas_equalTo((60+64)*KScreenHeightScale1334);
                
            }else{
                make.centerX.mas_equalTo(0);
                make.top.mas_equalTo((60+64)*KScreenHeightScale1334);
                
            }
        }else{
            make.centerX.mas_equalTo(0);
            make.top.mas_equalTo((60+64)*KScreenHeightScale1334);
            
        }
        
    }];
    titlePinLabel.text = Localized(@"选择你的Pin");
    titlePinLabel.font = Kfont(48);
    
    //六个显示出来的橙色圆圈
    self.sixPointView = [[UIView alloc] init];
    [self.view addSubview:self.sixPointView];
    
    [self.sixPointView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titlePinLabel.mas_bottom).offset(headBtnTop);
        make.left.mas_equalTo(self.view).offset(0);
        make.right.mas_equalTo(self.view).offset(0);
        make.height.mas_equalTo(15);
    }];
    
    //六个显示出来的红色圆圈
    for (NSInteger i=0; i<6; i++)
    {
        UIButton *headBtn = [[UIButton alloc] init];
        [headBtn setBackgroundImage:[UIImage imageNamed:@"grey_dash"] forState:UIControlStateNormal];
        
        headBtn.layer.cornerRadius = 7.5;
        headBtn.layer.masksToBounds = YES;
        headBtn.tag = 10+i;
        [_sixPointView addSubview:headBtn];
        
        [headBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_sixPointView.mas_top).offset(0);
            make.left.mas_equalTo(self.view).offset(leftWidth+i*15+i*twoBtnSep);
            make.height.width.mas_equalTo(15);
        }];
    }
    UIView *lineView = [[UIView alloc] init];
    lineView.backgroundColor = RGB(26, 31, 37);
    [self.view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(lineViewTop);
        make.left.mas_equalTo(self.view).offset(kSpacingDistance);
        make.right.mas_equalTo(self.view).offset(-kSpacingDistance);
        make.height.mas_equalTo(1);
    }];
    lineView.hidden = YES;
    CGFloat grayLeftOrigin;//最左边按钮起始位置
    CGFloat btnViewWidth;//按钮底部的View的宽高
    CGFloat separateWidth;//两个View之间横向的间隙
    CGFloat separateHeight;//两个View之间纵向的间隙
    CGFloat topBtnView;
    CGFloat viewBtnSep;//两个圆之间的距离
    if(iPhone4)
    {
        topBtnView = kDistance50*i5_i6H;
        grayLeftOrigin = 105/2*i5_i6W;
        separateWidth = kDistance30*i5_i6W;
        separateHeight = kDistance30*i5_i6H;
        btnViewWidth = (SCREEN_WIDTH/2-grayLeftOrigin-separateWidth)/1.5;
        viewBtnSep = kDistance5*i5_i6H;
    }
    else if (iPhone5)
    {
        topBtnView = kDistance50*i5_i6H;
        grayLeftOrigin = 105/2*i5_i6W;
        separateWidth = kDistance30*i5_i6W;
        separateHeight = kDistance30*i5_i6H;
        btnViewWidth = (SCREEN_WIDTH/2-grayLeftOrigin-separateWidth)/1.5;
        viewBtnSep = kDistance5*i5_i6H;
    }else if(iPhone6)
    {
        topBtnView = kDistance50;
        grayLeftOrigin = 105/2;
        separateWidth = kDistance30;
        separateHeight = kDistance30;
        btnViewWidth = (SCREEN_WIDTH/2-grayLeftOrigin-separateWidth)/1.5;
        viewBtnSep = kDistance5;
    }else if(iPhone6p)
    {
        topBtnView = kDistance50*i6p_6H;
        grayLeftOrigin = 105/2*i6p_6W;
        separateWidth = kDistance30*i6p_6W;
        separateHeight = kDistance30*i6p_6H;
        btnViewWidth = (SCREEN_WIDTH/2-grayLeftOrigin-separateWidth)/1.5;
        viewBtnSep = kDistance5*i6p_6H;
    }else
    {
        topBtnView = kDistance50*iX_i6H;
        grayLeftOrigin = 105/2*iX_i6W;
        separateWidth = kDistance30*iX_i6W;
        separateHeight = kDistance30*iX_i6H;
        btnViewWidth = (SCREEN_WIDTH/2-grayLeftOrigin-separateWidth)/1.5;
        viewBtnSep = kDistance5*iX_i6H;
    }
    
    // 下面12个按钮
    for (NSInteger i=0; i<3; i++)
    {
        for (NSInteger j=0; j<4; j++)
        {
            UIView *btnView = [[UIView alloc] init];
            btnView.backgroundColor = HEXCOLOR(0xcccccc);
            btnView.layer.cornerRadius = btnViewWidth/2;
            [self.view addSubview:btnView];
            
            [btnView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(lineView.mas_bottom).offset(topBtnView+j*(btnViewWidth+separateHeight));
                make.left.mas_equalTo(self.view).offset(grayLeftOrigin+i*(btnViewWidth+separateWidth));
                make.height.width.mas_equalTo(btnViewWidth);
            }];
            
            UIButton *grayBtn = [[UIButton alloc] init];
            grayBtn.backgroundColor = kWhiteColor;
            grayBtn.layer.cornerRadius = (btnViewWidth-2*viewBtnSep)/2;
            [grayBtn setTitle:self.numPointArr[3*j+i] forState:UIControlStateNormal];
            grayBtn.layer.masksToBounds = YES;
            
            
            grayBtn.tag = 100+j*3+i;
            grayBtn.titleLabel.font = Kfont(22);
            if (grayBtn.tag == 109||grayBtn.tag == 111)
            {
                grayBtn.titleLabel.font = Kfont(60);
            }else
            {
                grayBtn.titleLabel.font = Kfont(60);
            }
            [grayBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
            [grayBtn setBackgroundImage:[UIImage imageNamed:@""] forState:UIControlStateHighlighted];
            [grayBtn setTitleColor:kBlackColor forState:UIControlStateNormal];
            [grayBtn setTitleColor:HEXCOLOR(0x660000) forState:UIControlStateHighlighted];
            
            [grayBtn addTarget:self action:@selector(grayBtnAction:) forControlEvents:UIControlEventTouchUpInside];
            [btnView addSubview:grayBtn];
            
            [grayBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(btnView.mas_top).offset(viewBtnSep);
                make.left.mas_equalTo(btnView.mas_left).offset(viewBtnSep);
                make.height.width.mas_equalTo(btnViewWidth-2*viewBtnSep);
            }];
        }
    }
    
    //忘记pin
    UIButton *forgetPinBtn = [UIButton new];
    [self.view addSubview:forgetPinBtn];
    [forgetPinBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(-70*KScreenHeightScale1334);
        make.centerX.mas_equalTo(0);
    }];
    [forgetPinBtn setTitle:Localized(@"忘记Pin?") forState:UIControlStateNormal];
    [forgetPinBtn setTitleColor:kRedOrangeColor forState:UIControlStateNormal];
    //指纹按钮
    UIButton *fingerPintBtn = [UIButton new];
    [self.view addSubview:fingerPintBtn];
    [fingerPintBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(forgetPinBtn.mas_top).offset(-10);
        make.centerX.mas_equalTo(0);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    [fingerPintBtn setBackgroundImage:[UIImage imageNamed:@"finger"] forState:(UIControlStateNormal)];
    if (!kStringIsEmpty(self.fingerToken)){
        
        fingerPintBtn.hidden = NO;
        
    }else{
        
       fingerPintBtn.hidden = YES;
    }
    
    //事件
    [forgetPinBtn addTarget:self action:@selector(forgetPinBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [fingerPintBtn addTarget:self action:@selector(fingerPintBtnClick) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark-指纹支付
-(void)setFingerPay{
    
    [self showChrysanthemumAlert:nil];
    NSMutableDictionary *dict =[NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *signArr = [FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",signArr[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",signArr[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    [dict setObject:[FCLocalUserHelper getXidStr] forKey:@"XID"];
    [dict setObject:self.walletId forKey:@"walletId"];
    [dict setObject:self.addressStr forKey:@"address"];
    [dict setObject:self.moneyCountStr forKey:@"amount"];
    [dict setObject:self.fingerToken forKey:@"fingerToken"];
    NSString *url = Send_money_finger_URL;
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        if (responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                NSLog(@"%@",responseObject);
                [self hideProgress];
//                [FCLocalUserHelper pushMainTabbarController];
                
//                FCTransactionHistoryDetailModel *detailModel = self.orderArr[indexPath.row];
                 [self pushHistoryVC:responseObject[@"result"][@"orderId"]];
                
            }else if ([responseObject[@"code"]integerValue] == 400){
                [self hideProgress];
                [self showAlert:Localized(@"转账失败")];
            }
            else if ([responseObject[@"code"]integerValue] == 600){
                [self hideProgress];
                [self showAlert:Localized(@"余额不足")];
            }else if ([responseObject[@"code"]integerValue] == 405){
                [self hideProgress];
                [self showAlert:Localized(@"指纹已失效，请用密码支付")];
                [self.PINArrM removeAllObjects];
            }else if ([responseObject[@"code"]integerValue] == 417){
                [self hideProgress];
                [self showAlert:Localized(@"交易数量低于“发起最小额度”0.01,请调整后重试！")];
                [self.navigationController popViewControllerAnimated:YES];
            }else if ([responseObject[@"code"]integerValue] == 414){
                [self hideProgress];
                [self showAlert:Localized(@"金额格式错误")];
                [self.navigationController popViewControllerAnimated:YES];
            }else if ([responseObject[@"code"]integerValue] == 404){
                [self showAlert:Localized(@"地址错误，请修改后重试!")];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
        
        
    } faile:^(NSError *error) {
         [self showAlert:Localized(@"转账失败")];
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
}
-(void)fingerPintBtnClick{
    
    __weak typeof(self) weakSelf = self;
    
    LAContext *myContext = [[LAContext alloc] init];
    // 这个属性是设置指纹输入失败之后的弹出框的选项
    myContext.localizedFallbackTitle = @"";
    
    if (is_IOS_10)
    {
        myContext.localizedCancelTitle=Localized(@"取消");
    }
    NSError *authError = nil;
    NSString *myLocalizedReasonString = Localized(@"请验证已有指纹，用于支付");
    // 判断用户手机系统是否是 iOS 8.0 以上版本
    if (!([UIDevice currentDevice].systemVersion.floatValue >= 8.0)) {
        NSLog(@"系统不支持");
        return;
    }
    
    // MARK: 判断设备是否支持指纹识别
    if ([myContext canEvaluatePolicy:LAPolicyDeviceOwnerAuthentication error:&authError])
    {
        [myContext evaluatePolicy:LAPolicyDeviceOwnerAuthentication localizedReason:myLocalizedReasonString reply:^(BOOL success, NSError * _Nullable error) {
            if(success)
            {
                NSLog(@"指纹认证成功");
                //设置指纹认证
                [weakSelf setFingerPay];
                
            }
            else
            {
                
                NSLog(@"指纹认证失败，%@",error.description);
                
                NSLog(@"%ld", (long)error.code); // 错误码 error.code
                switch (error.code)
                {
                    case LAErrorAuthenticationFailed: // Authentication was not successful, because user failed to provide valid credentials
                    {
                        [self showAlert:Localized(@"尝试次数过多，请稍后重试")];
                        
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [weakSelf.navigationController popViewControllerAnimated:YES];
                        });
                        NSLog(@"授权失败"); // -1 连续三次指纹识别错误
                    }
                        break;
                    case LAErrorUserCancel: // Authentication was canceled by user (e.g. tapped Cancel button)
                    {
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [weakSelf.navigationController popViewControllerAnimated:YES];
                        });
                        NSLog(@"用户取消验证Touch ID"); // -2 在TouchID对话框中点击了取消按钮
                        
                    }
                        break;
                    case LAErrorUserFallback: // Authentication was canceled, because the user tapped the fallback button (Enter Password)
                    {
                        
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"用户选择输入密码，切换主线程处理"); // -3 在TouchID对话框中点击了输入密码按钮
                        }];
                        
                    }
                        break;
                    case LAErrorSystemCancel: // Authentication was canceled by system (e.g. another application went to foreground)
                    {
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [weakSelf.navigationController popViewControllerAnimated:YES];
                        });
                        NSLog(@"取消授权，如其他应用切入，用户自主"); // -4 TouchID对话框被系统取消，例如按下Home或者电源键
                    }
                        break;
                    case LAErrorPasscodeNotSet: // Authentication could not start, because passcode is not set on the device.
                        
                    {
                        [self showAlert:Localized(@"设备系统未设置密码")];
                        
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [weakSelf.navigationController popViewControllerAnimated:YES];
                        });
                        NSLog(@"设备系统未设置密码"); // -5
                    }
                        break;
                    case LAErrorTouchIDNotAvailable: // Authentication could not start, because Touch ID is not available on the device
                    {
                        [self showAlert:Localized(@"设备未设置Touch ID")];
                        
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [weakSelf.navigationController popViewControllerAnimated:YES];
                        });
                        NSLog(@"设备未设置Touch ID"); // -6
                    }
                        break;
                    case LAErrorTouchIDNotEnrolled: // Authentication could not start, because Touch ID has no enrolled fingers
                    {
                        
                        NSLog(@"用户未录入指纹"); // -7
                    }
                        break;
                        
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_9_0
                    case LAErrorTouchIDLockout: //Authentication was not successful, because there were too many failed Touch ID attempts and Touch ID is now locked. Passcode is required to unlock Touch ID, e.g. evaluating LAPolicyDeviceOwnerAuthenticationWithBiometrics will ask for passcode as a prerequisite 用户连续多次进行Touch ID验证失败，Touch ID被锁，需要用户输入密码解锁，先Touch ID验证密码
                    {
                        [self showAlert:Localized(@"Touch ID被锁，需要用户输入密码解锁")];
                        
                        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                            [weakSelf.navigationController popViewControllerAnimated:YES];
                        });
                        NSLog(@"Touch ID被锁，需要用户输入密码解锁"); // -8 连续五次指纹识别错误，TouchID功能被锁定，下一次需要输入系统密码
                        //跳转到系统密码设置界面
                        
                    }
                        break;
                    case LAErrorAppCancel: // Authentication was canceled by application (e.g. invalidate was called while authentication was in progress) 如突然来了电话，电话应用进入前台，APP被挂起啦");
                    {
                        
                        NSLog(@"用户不能控制情况下APP被挂起"); // -9
                    }
                        break;
                    case LAErrorInvalidContext: // LAContext passed to this call has been previously invalidated.
                    {
                        
                        NSLog(@"LAContext传递给这个调用之前已经失效"); // -10
                    }
                        break;
#else
#endif
                    default:
                    {
                        
                        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
                            NSLog(@"其他情况，切换主线程处理");
                        }];
                        break;
                    }
                }
            }
        }];
    }
    else
    {
        
        NSLog(@"设备不支持指纹");
        NSLog(@"%ld", (long)authError.code);
        
        switch (authError.code)
        {
            case LAErrorTouchIDNotEnrolled:
            {
                
                
                
                [self showAlert:Localized(@"身份验证无法启动,因为没有登记的手指触摸ID")];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                });
                
                NSLog(@"Authentication could not start, because Touch ID has no enrolled fingers");
                break;
            }
            case LAErrorPasscodeNotSet:
            {
                
                [self showAlert:Localized(@"身份验证无法启动,因为密码没有设置在设备上")];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                });
                NSLog(@"Authentication could not start, because passcode is not set on the device");
                break;
            }
            default:
            {
                [self showAlert:Localized(@"TouchID不可用，需要用户输入密码解锁")];
                
                dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                    [weakSelf.navigationController popViewControllerAnimated:YES];
                });
                NSLog(@"TouchID not available");
                break;
            }
        }
    }
    
    
}
- (void)grayBtnAction:(UIButton *)grayBtn
{
    
    if (grayBtn.tag == 109)
    {
        //CLR按钮，清除全部
        if (self.PINArrM.count>0)
        {
            [self.PINArrM removeAllObjects];
        }
        
    }else if (grayBtn.tag == 111)
    {
        //DEL按钮
        if (self.PINArrM.count>0)
        {
            [self.PINArrM removeLastObject];
        }
    }else
    {
        //数字
        NSNumber *number;
        if (grayBtn.tag==110)
        {
            //0
            number = [NSNumber numberWithInteger:0];
        }else
        {
            //1-9
            number = [NSNumber numberWithInteger:grayBtn.tag-99];
        }
        if (self.PINArrM.count>=6)
        {
            return;
        }else
        {
            [self.PINArrM addObject:number];
        }
    }
    
    //刷新上面4个橙色按钮
    [self refreshHeadBtnView:self.PINArrM.count];
    
}
//刷新上面六个橙色按钮
- (void)refreshHeadBtnView:(NSInteger)pinCount
{
    for (NSInteger i=0; i<6; i++)
    {
        UIButton *btn = [self.view viewWithTag:10+i];
        [btn setBackgroundImage:[UIImage imageNamed:@"grey_dash"] forState:UIControlStateNormal];
        btn.backgroundColor = [UIColor clearColor];
    }
    for (NSInteger i=0; i<pinCount; i++)
    {
        UIButton *btn = [self.view viewWithTag:10+i];
        [btn setBackgroundImage:[UIImage imageNamed:@"blue_dot"] forState:UIControlStateNormal];
    }
    if (pinCount >= 6) {
        [self showChrysanthemumAlert:nil];
        NSString *pinStr = [self.PINArrM componentsJoinedByString:@""];
        //发送交易
        [self sendMoney:pinStr];
        
        
    }
}
-(void)sendMoney:(NSString*)pinStr{
    
    [self showChrysanthemumAlert:nil];
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *signArr = [FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",signArr[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",signArr[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    [dict setObject:[FCLocalUserHelper getXidStr] forKey:@"XID"];
    [dict setObject:self.walletId forKey:@"walletId"];
    [dict setObject:self.addressStr forKey:@"address"];
    [dict setObject:self.moneyCountStr forKey:@"amount"];
    [dict setObject:[FCLocalUserHelper sha1:pinStr] forKey:@"PIN"];
    NSString *url = Send_money_URL;
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        if (responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
                NSLog(@"%@",responseObject);
                [self hideProgress];
//                [self showAlert:Localized(@"成功")];
                [self pushHistoryVC:responseObject[@"result"][@"orderId"]];
                
                
            }else if ([responseObject[@"code"]integerValue] == 400){
                [self hideProgress];
                [self showAlert:Localized(@"转账失败")];
            }
            else if ([responseObject[@"code"]integerValue] == 600){
                [self hideProgress];
                [self showAlert:Localized(@"余额不足")];
            }else if ([responseObject[@"code"]integerValue] == 406){
                [self hideProgress];
                [self showAlert:Localized(@"Pin不正确，请重试！")];
                [self clearPinColor];
            }else if ([responseObject[@"code"]integerValue] == 417){
                [self hideProgress];
                [self showAlert:Localized(@"交易数量低于“发起最小额度”0.01,请调整后重试！")];
                [self.navigationController popViewControllerAnimated:YES];
            }else if ([responseObject[@"code"]integerValue] == 409){
                [self hideProgress];
                [self showAlert:Localized(@"PIN错误次数太多，请重设PIN后重试！")];
                
            }else if ([responseObject[@"code"]integerValue] == 404){
                [self showAlert:Localized(@"地址错误，请修改后重试!")];
                [self.navigationController popViewControllerAnimated:YES];
            }else if ([responseObject[@"code"]integerValue] == 414){
                [self hideProgress];
                [self showAlert:Localized(@"金额格式错误")];
                [self.navigationController popViewControllerAnimated:YES];
            }
        }
        
    } faile:^(NSError *error) {
        if (error) {
            NSLog(@"%@",error);
            [self showAlert:Localized(@"转账失败")];
            [self.navigationController popViewControllerAnimated:YES];
        }
        
    }];
}
-(void)pushHistoryVC:(NSString*)orderID{
    NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithCapacity:1];
    NSArray *signArr = [FCLocalUserHelper getTimeAndSign];
    NSString *signStr = [NSString stringWithFormat:@"%@",signArr[1]];
    NSString *timeStr = [NSString stringWithFormat:@"%@",signArr[0]];
    [dict setObject:[FCLocalUserHelper sha1:signStr] forKey:@"sign"];
    [dict setObject:timeStr forKey:@"time"];
    [dict setObject:[FCLocalUserHelper getXidStr] forKey:@"XID"];
    [dict setObject:self.walletId forKey:@"walletId"];
    [dict setObject:@"1" forKey:@"page"];
    NSString *url = Get_wallet_histroy_URL;
    [FCSharedNetworkTool POSTWithURLString:url parameters:dict success:^(id responseObject) {
        if (responseObject) {
            if ([responseObject[@"code"] integerValue] == 200) {
               FCTransactionHistoryModel *transactionHistoryModel = [FCTransactionHistoryModel mj_objectWithKeyValues:responseObject[@"result"]];
                if (!kArrayIsEmpty(transactionHistoryModel.orders)) {
                    NSArray *orderArr = transactionHistoryModel.orders;
                    NSMutableArray *arrM = [NSMutableArray new];
                    [orderArr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                        
                        FCTransactionHistoryDetailModel *detailModel = [FCTransactionHistoryDetailModel mj_objectWithKeyValues:obj];
                        [arrM addObject:detailModel];
                        
                    }];
                    FCTransactionHistoryVC *vc = [FCTransactionHistoryVC new];
                    vc.isOrder = YES;
                    vc.walletId = self.walletId;
                    vc.orderStr = orderID;
                    vc.currentResultModel = self.currentResultModel;
                    vc.transactionHistoryDetailModel = arrM[0];
                    [self.navigationController pushViewController:vc animated:YES];
                    
                    
                }
                
            }
        }
        
        
    } faile:^(NSError *error) {
        
        
    }];
    
}
- (void)clearPinColor
{
    for (NSInteger i=0; i<6; i++)
    {
        UIButton *btn = [self.view viewWithTag:10+i];
        btn.backgroundColor = [UIColor clearColor];
        [btn setBackgroundImage:[UIImage imageNamed:@"grey_dash"] forState:UIControlStateNormal];
    }
    [self.PINArrM removeAllObjects];
}
#pragma mark-设置导航栏
-(void)setNav{
    self.navigationController.navigationBarHidden = YES;
    _viewNav =[UIView new];
    [self.view addSubview:_viewNav];
    [_viewNav mas_makeConstraints:^(MASConstraintMaker *make) {
        if (@available(iOS 11.0, *)) {
            if (iPhoneX) {
                
                make.left.right.top.mas_equalTo(self.view.safeAreaInsets);
                make.height.mas_equalTo(88);
            }else{
                make.left.right.mas_equalTo(0);
                make.height.mas_equalTo(64);
                make.top.mas_equalTo(0);
            }
        }else{
            make.left.right.mas_equalTo(0);
            make.height.mas_equalTo(64);
            make.top.mas_equalTo(0);
        }
        
    }];
    _viewNav.backgroundColor = [UIColor clearColor];
    
    UIButton *leftBtn = [UIButton new];
    [self.view addSubview:leftBtn];
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15*KScreenWidthScale750);
        make.centerY.mas_equalTo(_viewNav.mas_centerY).offset(8);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    [leftBtn setImage:[UIImage imageNamed:@"icon_back_black"] forState:UIControlStateNormal];
    [leftBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    
    //事件
    [leftBtn addTarget:self action:@selector(leftBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)leftBtnClick{
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark-懒加载
-(NSMutableArray *)PINArrM
{
    if (!_PINArrM)
    {
        _PINArrM = [NSMutableArray array];
    }
    return _PINArrM;
}
@end

